import Vue from "vue";
import VueRouter from "vue-router";
import BaseLayout from '@/layouts/BaseLayout.vue'

Vue.use(VueRouter);

const user=JSON.parse(window.localStorage.getItem("e-wallet-user"));

const routes = [
    {
        path: "/login",
        name: "login",
        component:()=>import("@/views/login/index.vue")
    },{
        path:"/",
		name:"origin",
		//redirect:"/login"       //这种设计要求当你访问首页一定要登录，
        beforeEnter(to, from, next){    //先改由路由守卫实现
            if(user && user.username && user.token){
                next("/home/index")
            }else{
                return next("/login");
            }
        }
    },{
        path:"/home",
        name:"home",
        component:BaseLayout, 
        children:[{
                path:"index",    //以“/”开头的嵌套路径会被当作根路径，所以子路由上不用加“/”，spring mvc不是这么用的，操蛋;
                name:"index",
                component:()=>import("./../views/index/index.vue")   //此index是首页而不是默认导出文件，名称上是这样
            },{
                path:"supermarket",
                name:"supermarket",
                component:()=>import("@/views/markets/supermarket/index.vue")
            },{
                path:"taobao",
                name:"taobao",
                component:()=>import("@/views/markets/taobao/index.vue")
            },{
                path:"jd",
                name:"jd",
                component:()=>import("@/views/markets/jd/index.vue")
            },{
                path:"smartcontract",
                name:"smartcontract",
                component:(resolve,reject)=>require(['@/views/smartcontract/index.vue'],resolve)
            },{ //还有很多页面没有写
                path:"viser",
                name:"viser",
                component:()=>import("@/views/viser/index.vue")
            }
        ]
    }
]; //end of routes

const router = new VueRouter({
    mode: "history",    //默认值hash
    base: process.env.BASE_URL,
    routes,
});

//TODO：全局路由守卫
router.beforeEach((to, from, next) => {
	if(to.path==="/login"){
		return next()
	}
    if(user && user.username && user.token){
        next()
    }else{
        return next("/login");
    }
})

export default router;
